home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / shoot / athrust.lha / AmigaThrust / src / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-05  |  6.9 KB  |  366 lines

  1.  
  2. /* Written by Peter Ekberg, peda@lysator.liu.se */
  3.  
  4. #ifdef HAVE_CONFIG_H
  5. #include "config.h"
  6. #endif
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #ifndef AMIGA
  12. #include <malloc.h>
  13. #endif
  14. #include <math.h>
  15.  
  16. #include "keyboard.h"
  17. #include "thrust_t.h"
  18. #include "init.h"
  19. #include "fast_gr.h"
  20. #include "graphics.h"
  21. #include "things.h"
  22. #include "font5x5.h"
  23. #include "thrust.h"
  24. #include "gr_drv.h"
  25.  
  26. #ifdef HAVE_SOUND
  27. #include "soundIt.h"
  28. #define NUM_SAMPLES 5
  29. #if defined(HAVE_LINUX_SOUND)
  30. #define SAMPLE_RATE 11000
  31. #elif defined(HAVE_SUN_SOUND) || defined(HAVE_HP_SOUND) || defined (HAVE_PAULA_SOUND)
  32. #define SAMPLE_RATE 11025
  33. #endif
  34. #define NUM_CHANNELS 4
  35. #define CHAN_1 0
  36. #define CHAN_2 1
  37. #define CHAN_3 2
  38. #define CHAN_4 3
  39. #define SND_BOOM   0
  40. #define SND_BOOM2  1
  41. #define SND_HARP   2
  42. #define SND_THRUST 3
  43. #define SND_ZERO   4
  44. Sample snd[NUM_SAMPLES];
  45. #endif
  46.  
  47. void
  48. turnship(void)
  49. {
  50.   word i,j,k;
  51.  
  52.   for(k=0; k<4; k++)
  53.     for(i=0; i<16; i++)
  54.       for(j=0; j<16; j++)
  55.     *(ship+(5+k)*256 +     i*16+j)=
  56.       *(ship+(3-k)*256 +(15-j)*16+15-i);
  57.   for(i=0; i<4; i++) {
  58.     memcpy(shipstorage,ship+(i<<8),256);
  59.     memcpy(ship+(i<<8),ship+((8-i)<<8),256);
  60.     memcpy(ship+((8-i)<<8),shipstorage,256);
  61.   }
  62.   for(k=0; k<8; k++)
  63.     for(i=0; i<16; i++)
  64.       for(j=0; j<16; j++)
  65.     *(ship+(9+k)*256 +     i*16+j)=
  66.       *(ship+(7-k)*256 +     i*16+15-j);
  67.   for(k=0; k<15; k++)
  68.     for(i=0; i<16; i++)
  69.       for(j=0; j<16; j++)
  70.     *(ship+(17+k)*256+     i*16+j)=
  71.       *(ship+(15-k)*256+(15-i)*16+j);
  72. }
  73.  
  74. void
  75. makeshieldedship(void)
  76. {
  77.   word i,j,k;
  78.  
  79.   memcpy(shieldship, ship, 8192);
  80.   for(i=0; i<32; i++)
  81.     for(j=0; j<16; j++)
  82.       for(k=0; k<16; k++)
  83.     if(*(bin_shld+(j<<4)+k))
  84.       *(shieldship+(i<<8)+(j<<4)+k)=*(bin_shld+(j<<4)+k);
  85. }
  86.  
  87. void
  88. makefuelmap(byte *fuelmap)
  89. {
  90.   int i;
  91.  
  92.   memset(fuelmap, 0, 64*4);
  93.  
  94.   for(i=0; i<32; i++) {
  95.     *(fuelmap+4*(i+32)+(i>6)+(i>16)+(i>26)) = 255;
  96.     *(fuelmap+4*i+(i<27)+(i<17)+(i<7)) = 255;
  97.   }
  98. }
  99.  
  100. int
  101. initmem(void)
  102. {
  103.   int i;
  104.  
  105.   printf("Allocating memory...");
  106.  
  107.   bild=(byte *)malloc((long)PBILDX*PBILDY*2+16);
  108.   bana=(byte *)malloc(maxlenx*maxleny);
  109.   ship=(byte *)malloc(8192);
  110.   shieldship=(byte *)malloc(8192);
  111.   shipstorage=(byte *)malloc(256);
  112.   bulletmap=(byte *)malloc(256);
  113.   bulletstorage=(byte *)malloc(maxbullets*16);
  114.   fragmentstorage=(byte *)malloc(maxfragments*4);
  115.   fuelmap=(byte *)malloc(256);
  116.   fuelstorage=(byte *)malloc(256);
  117.   loadmap=(byte *)malloc(64);
  118.   loadstorage=(byte *)malloc(64);
  119.   wirestorage=(byte *)malloc(64);
  120.  
  121.   if(!bild || !bana || !ship || !shieldship || !shipstorage
  122.      || !bulletmap || !bulletstorage || !fragmentstorage
  123.      || !fuelmap || !fuelstorage || !loadmap || !loadstorage
  124.      || !wirestorage) {
  125.     printf("failed!.\n");
  126.     return(0);
  127.   }
  128.   printf("done.\n");
  129.  
  130.   blocks=bin_blks;
  131.   memcpy(ship, bin_ship, 256*5);
  132.   for(i=0; i<16; i++)
  133.     memcpy(bulletmap+((20-i)&15)*16, bin_bullet+i*16, 16);
  134.  
  135.   for(i=0; i<title_cols*title_rows; i++)
  136.     *(title_pixels+i) += 192;
  137.  
  138.   memcpy(bin_colors+192*3, title_colors, title_nr_colors*3);
  139.  
  140.   for(i=0; i<3*256; i++)
  141.     bin_colors[i]=GAMMA(bin_colors[i]);
  142.  
  143.   printf("Turning the ship...");
  144.   turnship();
  145.   printf("done.\n");
  146.   printf("Building graphics...");
  147.   makefuelmap(fuelmap);
  148.   memcpy(loadmap, blocks+64*109, 64);
  149.   makeshieldedship();
  150.   printf("done.\n");
  151.  
  152.   return(1);
  153. }
  154.  
  155. void
  156. inithardware(int argc, char **argv)
  157. {
  158. #ifdef HAVE_SOUND
  159.   if(play_sound)
  160.     if(initsoundIt())
  161.       play_sound=0;
  162.     else
  163.       play_sound=1;
  164. #endif
  165.  
  166.   if(graphicsinit(argc, argv))
  167.     exit(-1);
  168.  
  169.   if(keyinit()) {
  170.     printf("Could not initialize the keyboard.\n");
  171.     exit(-1);
  172.   }
  173.   printf("Keyboard initialized.\n");
  174. }
  175.  
  176. void
  177. initscreen(int round)
  178. {
  179.   int i,j;
  180.  
  181.   if(round&2) {
  182.     bin_colors[65*3+0]=0;
  183.     bin_colors[65*3+1]=0;
  184.     bin_colors[65*3+2]=0;
  185.   }
  186.   else {
  187.     bin_colors[65*3+0]=GAMMA(colorr);
  188.     bin_colors[65*3+1]=GAMMA(colorg);
  189.     bin_colors[65*3+2]=GAMMA(colorb);
  190.   }
  191.  
  192.   for(j=pblocky; j<BBILDY+pblocky; j++)
  193.     for(i=pblockx; i<BBILDX+pblockx; i++)
  194.       writeblock(i%lenx, j, *(bana+i%lenx+j*lenx));
  195. }
  196.  
  197. void
  198. initgame(int round, int reset, int xblock, int yblock)
  199. {
  200.   int i;
  201.  
  202.   crash=0;
  203.   shoot=0;
  204. #ifdef DEBUG
  205.   repetetive=1;
  206. #else
  207.   repetetive=0;
  208. #endif
  209.   refueling=0;
  210.   speedx=0;
  211.   speedy=0;
  212.   absspeed=0L;
  213.   oldabs=0L;
  214.   vx=0;
  215.   vy=0;
  216.   if(round&1) {
  217.     kdir=72;
  218.     dir=24;
  219.     gravity=-20;
  220.     alpha=3*M_PI/2;
  221.     deltaalpha=0;
  222.   }
  223.   else {
  224.     kdir=24;
  225.     dir=8;
  226.     gravity=20;
  227.     alpha=M_PI/2;
  228.     deltaalpha=0;
  229.   }
  230.   if(reset) {
  231.     loaded=0;
  232.     loadcontact=0;
  233.     loadpoint=0;
  234.     loadpointshift=0;
  235.     shipdx=0;
  236.     shipdy=0;
  237.   }
  238.   else {
  239.     loadcontact=0;
  240.     if(loaded) {
  241.       loadpoint=126;
  242.       loadpointshift=0;
  243.       shipdx=(int)( cos(alpha)*loadpoint/5.90625);
  244.       shipdy=(int)(-sin(alpha)*loadpoint/5.90625);
  245.     }
  246.     else {
  247.       loadpoint=0;
  248.       loadpointshift=0;
  249.       *(bana+lenx*loadby+loadbx)=109;
  250.       shipdx=0;
  251.       shipdy=0;
  252.     }
  253.   }
  254.   
  255.   pblockx=xblock;
  256.   pblocky=yblock+4*(round&1);
  257.   if(loaded) {
  258.     if(round&1)
  259.       pblocky-=2;
  260.     else
  261.       pblocky+=2;
  262.   }
  263.  
  264.   pixx=pblockx<<3;
  265.   pixy=pblocky<<3;
  266.   x=pixx<<3;
  267.   y=pixy<<3;
  268.   bildx=(pixx+PBILDX-4)%PBILDX+4;
  269.   bildy=pixy%PBILDY;
  270.   bblockx=bildx>>3;
  271.   bblocky=bildy>>3;
  272.  
  273.   countdown=0;
  274.  
  275.   for(i=0; i<maxbullets; i++)
  276.     bullets[i].life=0;
  277.   for(i=0; i<maxfragments; i++)
  278.     fragments[i].life=0;
  279.  
  280.   chcolor=TEXTCOLOR;
  281.   chpaper=0;
  282.   chflag=0;
  283. }
  284.  
  285. #ifdef HAVE_SOUND
  286. int
  287. initsoundIt(void)
  288. {
  289.   printf("Initializing soundIt library v" SOUNDIT_VERS "...");
  290.   fflush(stdout);
  291.  
  292.   snd[0].data = sound_boom;
  293.   snd[0].len  = sound_boom_len;
  294.   snd[0].loop = 0;
  295.   snd[1].data = sound_boom2;
  296.   snd[1].len  = sound_boom2_len;
  297.   snd[1].loop = 0;
  298.   snd[2].data = sound_harp;
  299.   snd[2].len  = sound_harp_len;
  300.   snd[2].loop = 0;
  301.   snd[3].data = sound_thrust;
  302.   snd[3].len  = sound_thrust_len;
  303.   snd[3].loop = 1;
  304.   snd[4].data = sound_zero;
  305.   snd[4].len  = sound_zero_len;
  306.   snd[4].loop = 0;
  307.  
  308. #if defined(HAVE_LINUX_SOUND)
  309.   if(Snd_init(NUM_SAMPLES, snd, SAMPLE_RATE, NUM_CHANNELS, "/dev/dsp")
  310. #elif defined(HAVE_SUN_SOUND) || defined(HAVE_HP_SOUND)
  311.   if(Snd_init(NUM_SAMPLES, snd, SAMPLE_RATE, NUM_CHANNELS, "/dev/audio")
  312. #elif defined(HAVE_PAULA_SOUND)
  313.   if(Snd_init(NUM_SAMPLES, snd, SAMPLE_RATE, NUM_CHANNELS, "audio.device")
  314. #endif
  315.      == EXIT_FAILURE) {
  316.     printf("No sound.\n");
  317.     return(-1);
  318.   }
  319.  
  320.   printf("done.\n");
  321.   return(0);
  322. }
  323.                                                             
  324. void
  325. restoresoundIt(void)
  326. {
  327.   printf("Restoring sound...");
  328.   Snd_restore();
  329.   printf("done.\n");
  330. }
  331. #endif
  332.          
  333. void
  334. restorehardware(void)
  335. {
  336.   printf("Releasing keyboard...");
  337.   keyclose();
  338.   printf("done.\n");
  339.  
  340.   graphicsclose();
  341.  
  342. #ifdef HAVE_SOUND
  343.   if(play_sound)
  344.     restoresoundIt();
  345. #endif
  346. }
  347.  
  348. void
  349. restoremem(void)
  350. {
  351.   printf("Freeing allocated memory...");
  352.   free(bild);
  353.   free(bana);
  354.   free(bulletmap);
  355.   free(ship);
  356.   free(shieldship);
  357.   free(shipstorage);
  358.   free(fragmentstorage);
  359.   free(wirestorage);
  360.   free(fuelmap);
  361.   free(fuelstorage);
  362.   free(loadmap);
  363.   free(loadstorage);
  364.   printf("done.\n");
  365. }
  366.